package model.dao;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import model.dto.Manager;
import utility.Database;
import utility.DateTool;
import utility.Log;

public class ManagerModel extends Database {
	public boolean insert(Manager dto) {
		String sql = "insert into tbl_manager(manager_id,manager_name,manager_date_of_birth,manager_image,country_id) values(null,?,?,?,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setDate(2, (java.sql.Date)dto.getDateOfBirth());
			pstmt.setString(3, dto.getImage());
			pstmt.setInt(4, dto.getCountryId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int country_id) {
		String sql = "delete from tbl_manager where manager_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, country_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Manager dto) {
		String sql = "update tbl_manager set manager_name=?,manager_date_of_birth=?,manager_image=?,country_id=? where manager_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setDate(2, (java.sql.Date)dto.getDateOfBirth());
			pstmt.setString(3, dto.getImage());
			pstmt.setInt(4, dto.getCountryId());
			pstmt.setInt(5, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Manager get(int manager_id) {
		String sql = "select * from tbl_manager where manager_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, manager_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Manager dto = new Manager();
				dto.setId(manager_id);
				dto.setName(rs.getString("manager_name"));;
				dto.setDateOfBirth(rs.getDate("manager_date_of_birth"));
				dto.setImage(rs.getString("manager_image"));
				dto.setCountryId(rs.getInt("country_id"));
				return dto;
			}
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_manager where manager_name like ? ORDER BY manager_id DESC limit ?,?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(int limit, int page) {
		return list("", limit, page);
	}

	public int count(String search) {
		String sql = "select count(*) from tbl_manager where manager_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return 0;
	}

	public int count() {
		return count("");
	}
public static void main(String[] args) {
	Manager dto=new Manager();
	ManagerModel dao=new ManagerModel();
	
	dto.setId(1);
	dto.setName("Dara77");
	dto.setImage("dara.png77");
	dto.setCountryId(13);
	//SimpleDateFormat df=new SimpleDateFormat("yyyy/MM/dd");
	String date="2011/09/8";
	dto.setDateOfBirth(DateTool.getDateSQL(date));
	//java.util.Date x;
	/*try {
		x = df.parse(date);
		dto.setDateOfBirth(new java.sql.Date(x.getTime()));
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}*/
	
		
	
	System.out.println(dao.insert(dto));
	//System.out.println(dao.update(dto));
	//System.out.println(dao.delete(1));
	//System.out.println(dao.get(6).getDateOfBirth());
}

}
